import { createApp } from 'vue'
import { createPinia } from 'pinia'
import { InfiniteLoading } from '@nutui/nutui'
import "@nutui/nutui/dist/style.css";
import './assets/main.css'
import * as vant from 'vant';
import App from './App.vue'
import router from './router'
import axios from 'axios';
axios.defaults.baseURL = 'http://127.0.0.1:3000/shopfront'
import 'vant/lib/index.css';
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import '../iconfont/font_4666373_81k8o84bkss/iconfont.css'
import '../iconfont/font_4666373_81k8o84bkss/iconfont.js'

import piniaPersist from 'pinia-plugin-persist'
const pinia = createPinia()
pinia.use(piniaPersist)

// 在axios请求拦截器中使用token
axios.interceptors.request.use(config => {
  const accessToken = localStorage.getItem('accessToken');
  if (accessToken) {
    config.headers.Authorization = 'Bearer ' + accessToken;
  }
  return config;
}, error => {
  return Promise.reject(error);
});

// 当Access Token过期时，使用Refresh Token获取新的Access Token
axios.interceptors.response.use(response => {
  return response;
}, error => {
  const originalRequest = error.config;
  if (error.response.status === 401 && !originalRequest._retry) {
    originalRequest._retry = true;
    const refreshToken = localStorage.getItem('refreshToken');
    return axios.post('/refresh-token', { token: refreshToken })
      .then(response => {
        localStorage.setItem('accessToken', response.data.accessToken);
        axios.defaults.headers.common['Authorization'] = 'Bearer ' + response.data.accessToken;
        return axios(originalRequest);
      });
  }
  return Promise.reject(error);
});
import { TUICallKit } from "@tencentcloud/call-uikit-vue";


const app = createApp(App)

app.use(vant)
app.use(InfiniteLoading)
app.config.globalProperties.$axios = axios

app.use(TUICallKit)

app.use(ElementPlus)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
app.use(pinia)
app.use(router)

app.mount('#app')
